import cn.yunhe.dao.IBlockDao;
import cn.yunhe.dao.INewsDao;
import cn.yunhe.dao.IUserDao;
import cn.yunhe.entity.Block;
import cn.yunhe.entity.News;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.List;

public class Demo {
    ApplicationContext ac = null;
    IUserDao userDao = null;
    INewsDao newsDao = null;
    IBlockDao blockDao = null;


    @Before
    public void init() {
        ac = new ClassPathXmlApplicationContext("spring.xml");
        userDao = (IUserDao) ac.getBean("userDao");
        newsDao = (INewsDao) ac.getBean("newsDao");
        blockDao = (IBlockDao) ac.getBean("blockDao");
    }


    @Test
    public void add() {
        User u = new User("张三", "111", 30);
        userDao.addUser(u);
    }

    @Test
    public void login() {
        User u = new User();
        u.setName("张三");
        u.setPwd("111");

        User user = userDao.loginUser(u);
        System.out.println(user.getName());
    }

    @Test
    public void getNews() {
        News n = newsDao.getNews(1);
        System.out.println(n.getTitle() + "所属的版块：" + n.getBlock().getBlockName() + ",发布人：" + n.getAdmin().getAdminName());
    }

    @Test
    public void getBlock() {
        Block block = blockDao.getBlock(1);
        System.out.println("版块：" + block.getBlockName() + "下的文章有： ");

        Iterator<News> newsSet = block.getNewsSet().iterator();

        while (newsSet.hasNext()) {
            System.out.println(newsSet.next().getTitle());
        }

    }

    @Test
    public void getNewsAndOther() {
        List<News> newsList = newsDao.getAllNews();

        for (News n : newsList) {
            System.out.println(n.getTitle() + "," + n.getBlock().getBlockName() + "," + n.getAdmin().getAdminName());
        }

    }

    @Test
    public void getNewsByPage() {
        Page page = newsDao.getNewsByPage(2, 3);

        for (int i = 0; i < page.getDataList().size(); i++) {
            System.out.println(((News)page.getDataList().get(i)).getTitle());
        }
    }


}
